﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine;
using Esprima;
using Game;

namespace MFTrain
{
    public class SubsystemRodToTrainBehavior : SubsystemBlockBehavior
    {
        public override int[] HandledBlocks => new int[1]
        {
            RodBlock.Index
        };

        public override bool OnUse(Ray3 ray, ComponentMiner componentMiner)
        {
            object obj = componentMiner.Raycast(ray, RaycastMode.Digging);
            if (obj is BodyRaycastResult)
            {
                ComponentMFTrain train;
                if ((train = ((BodyRaycastResult)obj).ComponentBody.Entity.FindComponent<ComponentMFTrain>()) != null)
                {
                    train.ThrowAllItems();
                    Project.FindSubsystem<SubsystemPickables>().AddPickable(MFTrainBlock.Index, 1, ((BodyRaycastResult)obj).ComponentBody.Position + new Vector3(0, 0.5f, 0), null, null);
                    Project.RemoveEntity(((BodyRaycastResult)obj).ComponentBody.Entity, true);
                    Project.FindSubsystem<SubsystemAudio>().PlaySound("Audio/BlockPlaced", 1f, 0f, train.m_componentBody.Position, 3f, true);
                    return true;
                }
            }
            return base.OnUse(ray, componentMiner);
        }
    }
}
